<template>
    <div :class="className" :style="{ height: height, width: width }">
      <!-- 图表容器 -->
      <div ref="chartContainer" style="width: 100%; height: 100%;" />
    </div>
  </template>
  
  <script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  
    // 处理月份数据
    function padMonthData(expectedData, monthLabels) {
    if (expectedData.length >= 12) {
        return {
        expectedData: expectedData.slice(0, 12),
        monthLabels: monthLabels ? monthLabels.slice(0, 12) : ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
        }
    }

    const defaultMonthLabels = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
    
    const paddedData = [...expectedData, ...Array(12 - expectedData.length).fill(null)]
    const paddedLabels = monthLabels 
        ? [...monthLabels, ...defaultMonthLabels.slice(monthLabels.length)]
        : defaultMonthLabels

    return {
        expectedData: paddedData,
        monthLabels: paddedLabels
    }
    }
  
  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '350px'
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (this.chart) {
        this.chart.dispose()
      }
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$refs.chartContainer, 'macarons')
        this.setOptions(this.chartData)
      },
      setOptions({ expectedData, monthLabels } = {}) {
        const { expectedData: paddedData, monthLabels: paddedLabels } = padMonthData(expectedData, monthLabels)
        this.chart.setOption({
          xAxis: {
            data: paddedLabels || ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            boundaryGap: false,
            axisTick: {
              show: false
            },
            axisLabel: {
              rotate: 60 // 旋转60度
            }
          },
          grid: {
            left: 10,
            right: 20,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              formatter: function(value) { return value.toFixed(1) + 'mg/L' }
            }
          },
          legend: {
            data: ['化学需氧量']
          },
          series: [{
            name: '化学需氧量',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: paddedData,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }]
        })
      }
    }
  }
  </script>